package com.atguigu.web;

import java.io.IOException;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.atguigu.pojo.Cart;
import com.atguigu.pojo.User;
import com.atguigu.service.OrderService;
import com.atguigu.service.impl.OrderServiceImpl;

/**
 * Servlet implementation class OrderServlet
 */
public class OrderServlet extends BaseServlet implements Servlet {
	private static final long serialVersionUID = 1L;
	private OrderService orderService = new OrderServiceImpl();
	
	protected void saveOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//通过session来获取购物车对象和用户id
		Cart cart = (Cart) request.getSession().getAttribute("cart");
		User user = (User) request.getSession().getAttribute("user");
		if(user == null) {
			request.getRequestDispatcher("/pages/user/login.jsp").forward(request, response);
			return;
		}
		int userId = user.getId();
		
		//生成订单
		String orderId = orderService.saveOrder(cart, userId);
		//保存订单到request中
		request.setAttribute("orderId", orderId);
		
		request.getRequestDispatcher("/pages/cart/checkout.jsp").forward(request, response);
	}

}
